﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oex_gmn_translation
{
    class Program
    {
        static string DEFAULT_LANG = "http://google-mail-oex.googlecode.com/svn/trunk/locales/lang.default.js";
        static void Main(string[] args)
        {
            // Argument 1: Path to translation file
            // Argument 2: Path to default translation (optional, uses svn-trunk by default)
            if(args.Length < 1 || args.Length > 2)
            {
                ShowHelp();
                return;
            }

            // Try to open translation-file
            Dictionary<string, string> dic_trans = new Dictionary<string, string>();
            try
            {
                dic_trans = UniversalReader(args[0]);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR opening the translation-file");
                Console.WriteLine(ex.Message);
                return;
            }

            // Try to open default-file
            Dictionary<string, string> dic_default = new Dictionary<string, string>();
            try
            {
                if(args.Length == 1)
                    dic_default = UniversalReader(DEFAULT_LANG);
                else
                    dic_default = UniversalReader(args[1]);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR opening the translation-file");
                Console.WriteLine(ex.Message);
                return;
            }

            // Compare-Header
            Console.WriteLine("TRANSLATION: " + args[0]);
            if (args.Length == 1)
                Console.WriteLine("compared to default: " + DEFAULT_LANG);
            else
                Console.WriteLine("compared to default: " + args[1]);
            Console.WriteLine("");
            Console.WriteLine("--------------------------------------------------------------------------------");
            
            // Not Found in Transaltion
            List<string> error_keys = new List<string>();
            foreach (string key in dic_default.Keys)
            {
                if(!dic_trans.ContainsKey(key))
                    error_keys.Add(key);
            }
            if (error_keys.Count > 0)
            {
                Console.WriteLine("These " + error_keys.Count + " Strings-Keys are NOT in the Translation (ERROR):");
                Console.WriteLine("");
                foreach (string key in error_keys)
                    Console.WriteLine(key);
            }
            else
            {
                Console.WriteLine("All needed Strings-Keys are in the Translation :)");
            }
            Console.WriteLine("");
            Console.WriteLine("--------------------------------------------------------------------------------");

            // Not Found in Default
            error_keys = new List<string>();
            foreach (string key in dic_trans.Keys)
            {
                if (!dic_default.ContainsKey(key))
                    error_keys.Add(key);
            }
            if (error_keys.Count > 0)
            {
                Console.WriteLine("These " + error_keys.Count + " Strings-Keys are NOT in the Default-Translation (unneeded):");
                Console.WriteLine("");
                foreach (string key in error_keys)
                    Console.WriteLine(key);
            }
            else
            {
                Console.WriteLine("The are no unneeded Strings-Keys in the Translation :)");
            }
            Console.WriteLine("");
            Console.WriteLine("--------------------------------------------------------------------------------");

            // Same
            error_keys = new List<string>();
            foreach (string key in dic_default.Keys)
            {
                if (dic_trans.ContainsKey(key))
                    if(dic_default[key] == dic_trans[key])
                        error_keys.Add(key);
            }
            if (error_keys.Count > 0)
            {
                Console.WriteLine("These " + error_keys.Count + " Strings are similar as in the Default-Translation (just check):");
                Console.WriteLine("");
                foreach (string key in error_keys)
                    Console.WriteLine(key);
            }
            else
            {
                Console.WriteLine("The are no similar Strings in the Translations :)");
            }
            Console.WriteLine("");
            Console.WriteLine("--------------------------------------------------------------------------------");

            // Compare-Footer
            Console.WriteLine("ready - " + DateTime.Now.ToString());
        }

        // Opens local file or url
        static Dictionary<string, string> UniversalReader(string file)
        {
            Uri urifile = new Uri(file);
            Dictionary<string, string> dic = new Dictionary<string, string>();

            // Check if local or...
            System.IO.StreamReader sr;
            if (urifile.IsFile)
            {
                sr = new System.IO.StreamReader(file);
            }
            // ... url
            else
            {
               System.Net.WebRequest req = System.Net.WebRequest.Create(file);                
               System.Net.WebResponse resp = req.GetResponse();
               System.IO.Stream st = resp.GetResponseStream();
               sr = new System.IO.StreamReader(st);
            }

            // Read
            while (!sr.EndOfStream)
            {
                string line = sr.ReadLine();
                if (line.StartsWith("lang."))
                {
                    string key = line.Substring(0, line.IndexOf("="));
                    string content = line.Substring(line.IndexOf("="), line.Length - line.IndexOf("="));
                    dic.Add(key.Trim(), content.Trim());
                }
            }
            return dic;
        }

        // Show help
        static void ShowHelp()
        {
            Console.WriteLine("usage: oex-gmn-translation.exe <translation-file.js> (<default-translation.js>)");
            Console.WriteLine();
            Console.WriteLine("all paths must be absolute! URLs or local paths supported.");
            Console.WriteLine("if no <default-translation.js> is given wue use: ");
            Console.WriteLine("http://google-mail-oex.googlecode.com/svn/trunk/locales/lang.default.js");
        }


    }
}
